<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片选择与预览</title>
  <style>
    label {
      display: inline-block;
      padding: 10px 15px;
      background-color: #007bff;
      color: white;
      border-radius: 5px;
      cursor: pointer;
    }

    label:hover {
      background-color: #0056b3;
    }

    input[type="file"] {
      display: none;
    }

    img {
      border: 1px solid #ddd;
      border-radius: 5px;
      max-width: 300px;
      margin-top: 10px;
    }
  </style>
</head>

<body>
  <label for="imageUpload">选择图片</label>
  <input type="file" id="imageUpload" accept="image/*">
  <img id="previewImage" src="#" alt="预览图片" style="display:none;">

  <script>
    document.getElementById('imageUpload').addEventListener('change', function (event) {
      const file = event.target.files[0];
      console.log(file);
      console.log(typeof file);
      if (file) {
        const reader = new FileReader();
        reader.onload = function (e) {
          console.log(e.target.result);
          const previewImage = document.getElementById('previewImage');
          //e.target.result;就是图片的base64编码
          previewImage.src = e.target.result;
          previewImage.style.display = 'block';
        };
        reader.readAsDataURL(file);
      }
     
    });
  </script>
</body>

</html>